package com.mehdiuz.backend.role.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.api.R;
import com.mehdiuz.backend.commons.utils.JwtUtils;
import com.mehdiuz.backend.commons.utils.Result;
import com.mehdiuz.backend.commons.utils.ResultCodeEnum;
import com.mehdiuz.backend.role.entity.Menu;
import com.mehdiuz.backend.role.entity.User;
import com.mehdiuz.backend.role.entity.UserRole;
import com.mehdiuz.backend.role.entity.vo.RouterVo;
import com.mehdiuz.backend.role.entity.vo.UserVo;
import com.mehdiuz.backend.role.service.UserRoleService;
import com.mehdiuz.backend.role.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/system/index")
@Api(tags = "登录控制")
public class IndexController {

    @Autowired
    private UserService userService;

    /**
     * {"code":20000,"data":{"token":"admin-token"}}
     * @return
     */
    @PostMapping("login")
    @ApiOperation(value = "返回当前登录用户的token")
    public Result login(@RequestBody UserVo user){
        return userService.login(user);
    }

    /**
     * {"code":20000,"data":{"roles":["admin"],"introduction":"I am a super administrator","avatar":"https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif","name":"Super Admin"}}
     * @return
     */
    @GetMapping("info")
    @ApiOperation(value = "获取当前登录用户的权限")
    public Result info(HttpServletRequest request){
        String token = request.getHeader("token");
        if(StringUtils.isBlank(token)){
            return Result.fail().code(ResultCodeEnum.DATA_ERROR.getCode()).message(ResultCodeEnum.DATA_ERROR.getMessage());
        }
        String username = JwtUtils.getUsername(token);
        User user = new User();
        try {
            user = userService.getBaseMapper().selectOne(new QueryWrapper<User>().eq("username",username));
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail().code(ResultCodeEnum.DATA_ERROR.getCode()).message(ResultCodeEnum.DATA_ERROR.getMessage());
        }
        if(null == user){
            return Result.fail().code(ResultCodeEnum.DATA_ERROR.getCode()).message(ResultCodeEnum.DATA_ERROR.getMessage());
        }
        if(user.getStatus() == 0){
            return Result.fail().code(ResultCodeEnum.ACCOUNT_STOP.getCode()).message(ResultCodeEnum.ACCOUNT_STOP.getMessage());
        }
        //菜单树形路由
        //按钮
        Map<String,List> map = userService.getRouterTree(user.getId());
        List<Menu> menus = map.get("menus");
        List<RouterVo> routers = map.get("routers");
        List<String> permissionList = menus.stream().map(Menu::getPerms).filter(StringUtils::isNotBlank).collect(Collectors.toList());
        Map<String,Object> resMap = new HashMap<>();
        resMap.put("routers",routers);
        resMap.put("roles",  "");
        resMap.put("buttons",permissionList);
        resMap.put("name",user.getUsername());
        resMap.put("avatar",user.getHeadUrl());
        return Result.ok(resMap);
        //获取当前登录用户的个人信息，对应的角色，对应的目录，菜单，按钮权限信息
    }

    @PostMapping("logout")
    public Result logout(){
        return userService.logout();
    }
}
